#include <iostream>
#include <memory.h>
using namespace std;

#include "constant.h"
#include "ast_ami.h"
#include "ast_peg.h"

#include "CmdBase.h"
#include "CmdJoin.h"
#include "CmdLeave.h"
#include "CmdState.h"
#include "CmdDtmf.h"
#include "CmdHello.h"
#include "CmdFactory.h"

#include "ast_event.h"

CCmdBase * CCmdFactory::ParseLine( char *aLineBuf )
{
    char     * sCurLinePtr      = NULL;
    char     * sMatchedPtr      = NULL;
    CCmdBase * spData           = NULL;
    char     * sFieldCurLinePtr = NULL;
    char     * sFieldMatchedPtr = NULL;

    char       sContent[128];

    int        sRetVal = 0;

    sCurLinePtr = aLineBuf;

    if( event_f::Matches( sCurLinePtr, sMatchedPtr ) == true )
    {
        sFieldCurLinePtr = sMatchedPtr;
        if( conferencestate_f::Matches( sFieldCurLinePtr, sFieldMatchedPtr ) )
        {
            spData = new CCmdState();
            spData->setCmdType( cmd_type_event );
            spData->setEventType( cmd_event_conf_state );
            ((CCmdState*)spData)->parse(( char*)sFieldCurLinePtr );
            goto parse_success;
        }
        sFieldCurLinePtr = sMatchedPtr;
        if( conferenceleave_f::Matches( sFieldCurLinePtr, sFieldMatchedPtr ) )
        {
            spData = new CCmdLeave();
            spData->setCmdType( cmd_type_event );
            spData->setEventType( cmd_event_conf_leave );
            ((CCmdLeave*)spData)->parse(( char*)sFieldCurLinePtr );
            goto parse_success;
        }
        sFieldCurLinePtr = sMatchedPtr;
        if( conferencejoin_f::Matches( sFieldCurLinePtr, sFieldMatchedPtr ) )
        {
            spData = new CCmdJoin();
            spData->setCmdType( cmd_type_event );
            spData->setEventType( cmd_event_conf_join );
            ((CCmdJoin*)spData)->parse((char*) sFieldCurLinePtr );
            goto parse_success;
        }
        sFieldCurLinePtr = sMatchedPtr;
        if( conferencedtmf_f::Matches( sFieldCurLinePtr, sFieldMatchedPtr ) )
        {
            spData = new CCmdDtmf();
            spData->setCmdType(   cmd_type_event );
            spData->setEventType( cmd_event_conf_dtmf );
            ((CCmdDtmf*)spData)->parse( (char*)sFieldCurLinePtr );
            goto parse_success;
        }
        goto parse_fail;
    }

    sCurLinePtr = aLineBuf;
    if( response_f::Matches( sCurLinePtr, sMatchedPtr ) == true )
    {
        memset( sContent, 0, 32 );
        memcpy( sContent,
                sMatchedPtr + 9,
                ( sCurLinePtr - sMatchedPtr - 11 ) );
        //cmdtype = cmd_type_response;
        goto parse_success;
    }

    sCurLinePtr = aLineBuf;
    if( hello_f::Matches( sCurLinePtr, sMatchedPtr ) == true )
    {
        spData = new CCmdHello();

        if( spData != NULL )
        {
            spData->setCmdType( cmd_type_hello );
            spData->setEventType( cmd_event_unknown );
            ((CCmdHello*)spData)->parse( (char*) sMatchedPtr );
            printf( "%s\n", aLineBuf );
            sRetVal = ast_login( "admin", "amp111" );

            if( sRetVal == 1 )
            {
                goto parse_success;
            }
        }
    }

    goto parse_fail;

parse_success:
    return spData;

parse_fail:
    return NULL;
}

CCmdBase * CCmdFactory::CreateCmdBase( char * aLineBuf )
{
    return ParseLine( aLineBuf );
}
